package analyzer;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSDataSender {
	
	private static JMSDataSender instance;
    
    /**
     * Using Singleton pattern. 
     * @return 
     */
    public static JMSDataSender getInstance() {
        if (instance == null) {
                instance = new JMSDataSender();
                try{
                	instance.start();
                } catch (JMSException e) {
                	e.printStackTrace();
                }
                
        }
        return instance;
    }
	
	private ActiveMQConnectionFactory connectionFactory;
    private Connection connection;
    private Session session;
    private Destination destination1;
    private Destination destination2;

    private String subjectData = "ANALYZE";
    private String subjectCoords = "COORDS";

    private MessageProducer producerData;
    private MessageProducer producerCoords;
    
    private JMSDataSender() {
        String url = ActiveMQConnection.DEFAULT_BROKER_URL;
        // Name of the queue we will be sending messages to
        try{
            // Getting JMS connection from the server and starting it
            connectionFactory = new ActiveMQConnectionFactory(url);
            connection = connectionFactory.createConnection();
            connection.start();

            // JMS messages are sent and received using a Session. We will
            // create here a non-transactional session object. If you want
            // to use transactions you should set the first parameter to 'true'
            session = connection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);

            // Destination represents here our queue 'TESTQUEUE' on the
            // JMS server. You don't have to do anything special on the
            // server to create it, it will be created automatically.
            destination1 = session.createQueue(subjectData);
            destination2 = session.createQueue(subjectCoords);
            
            
            } catch (Exception e){
            	System.out.println(e);
        }
    }
	
	/**
     * sends string msg to the current Direction
     */
    public void sendData(String msg) throws JMSException{
        TextMessage message = session.createTextMessage(msg);
        producerData.send(message);
    }
    
    public void sendDriverCoordinates(String msg) throws JMSException{
    	TextMessage message = session.createTextMessage(msg);
        producerCoords.send(message);
    }
    
    public void close() throws JMSException{
        connection.close();
    }
    
    /**
     * starts producing and consuming messages using JMS
     */
    public void start() throws JMSException{
        producerData = session.createProducer(destination1);
        producerCoords = session.createProducer(destination2);
    }



}
